import { CONSTANTS } from "./CONSTANTS.js";
import { ChessMove } from "./chessmove.js";
export class ChessState {
  constructor(
    board = Array.from({ length: CONSTANTS.SIZE * CONSTANTS.SIZE }, () => null),
    to_move = null
  ) {
    this.board = board;
    this.to_move = to_move;
  }

  get game_result() {
    // 返回胜者
    let result = CONSTANTS.CONTINUE;

    if (CONSTANTS.SIZE === 3) {
      for (let [a, b, c] of CONSTANTS.wincombinations) {
        if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === CONSTANTS.X
        ) {
          result = CONSTANTS.X;
          break;
        } else if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === CONSTANTS.O
        ) {
          result = CONSTANTS.O;
          break;
        }
      }
    } else if (CONSTANTS.SIZE === 5) {
      for (let [a, b, c, d] of CONSTANTS.wincombinations_5x5) {
        if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === this.board[d] &&
          this.board[d] === CONSTANTS.X
        ) {
          result = CONSTANTS.X;
          break;
        } else if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === this.board[d] &&
          this.board[d] === CONSTANTS.O
        ) {
          result = CONSTANTS.O;
          break;
        }
      }
    } else if (CONSTANTS.SIZE === 4) {
      for (let [a, b, c] of CONSTANTS.wincombinations_4x4_3) {
        if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === CONSTANTS.X
        ) {
          result = CONSTANTS.X;
          break;
        } else if (
          this.board[a] === this.board[b] &&
          this.board[b] === this.board[c] &&
          this.board[c] === CONSTANTS.O
        ) {
          result = CONSTANTS.O;
          break;
        }
      }
    }

    if (
      result === CONSTANTS.CONTINUE &&
      this.get_legal_actions().length === 0
    ) {
      result = CONSTANTS.DRAW; // 平局
    }

    return result;
  }

  is_gameover() {
    return this.game_result !== CONSTANTS.CONTINUE;
  }

  is_move_legal(move) {
    if (this.is_gameover()) {
      // console.log("gameover");
      return false; // 游戏结束后不支持点击
    }
    if (this.to_move !== move.value) {
      return false;
    }

    if (!(0 <= move.pos && move.pos < CONSTANTS.SIZE * CONSTANTS.SIZE)) {
      return false;
    }

    if (this.board[move.pos] !== null) {
      return false;
    }

    return true;
  }

  get_legal_actions() {
    const validMoves = [];

    for (let i = 0; i < CONSTANTS.SIZE * CONSTANTS.SIZE; i++) {
      if (this.board[i] === null) {
        const move = new ChessMove(i, this.to_move);
        validMoves.push(move);
      }
    }

    return validMoves;
  }

  chessmove(move) {
    if (!this.is_move_legal(move)) {
      throw new Error("ERROR");
    }
    const newBoard = [...this.board]; // 使用扩展运算符进行深复制
    newBoard[move.pos] = move.value;

    let to_move = this.to_move === CONSTANTS.X ? CONSTANTS.O : CONSTANTS.X;

    return new ChessState(newBoard, to_move);
  }
}
